from flask import Flask, render_template
from pyecharts import options
from pyecharts.charts import Bar, Pie
from pyecharts.faker import Faker

app = Flask(__name__)


@app.route('/')
def index():
    c = (
        Bar()
            .add_xaxis(["衬衫", "羊毛衫", "雪纺衫"])
            .add_yaxis("商家A", [5, 20, 36])
            .add_yaxis("商家B", [15, 25, 16])
            .set_global_opts(title_opts=options.TitleOpts(title="我是主标题", subtitle="我是副标题"))
    )

    c1 = (
        Pie()
            .add("", [list(z) for z in zip(Faker.choose(), Faker.values())])
            .set_colors(["blue", "green", "yellow", "red", "pink", "orange", "purple"])
            .set_global_opts(title_opts=options.TitleOpts(title="Pie-设置颜色"))
            .set_series_opts(label_opts=options.LabelOpts(formatter="{b}: {c}"))
    )

    return render_template(
        'index.html',
        bar=c.dump_options(),
        pie=c1.dump_options(),
    )


if __name__ == "__main__":
    app.run(port=8005)
